package storage;

import static org.junit.Assert.*;

import java.io.File;
import java.util.Calendar;
import java.util.GregorianCalendar;

import memory.GenericEvents;
import memory.Memory;

import org.junit.Test;

public class TestStorage {
	
	//@author A0085082E
	@Test
	public void test() {
		
		testInvalidFileName_NewFileCreated();
		testReadFromFile_IO();
		//Prints "file not found" on console
		saveToStorage_IO();
		//Prints "problem creating file" on console
		testInvalidFileName_FileIO();
	}
	
	//Test IO exception
	private static void testInvalidFileName_FileIO(){
		
		Storage.setSaveFileAddress("");
		Memory testMem = new Memory();
		Storage.checkForFileExists(testMem);
		
	}
	
	//Check if new text is created
	private static void testInvalidFileName_NewFileCreated(){
		
		Storage.setSaveFileAddress("createNew.txt");
		Memory testMem = new Memory();
		Storage.checkForFileExists(testMem);
		
		File thisFile = new File("createNew.txt");
		assertEquals("Check if new file is created", thisFile.exists(), true);
		
	}
	
	private static void testReadFromFile_IO(){
		
		Storage.setSaveFileAddress("goodFile.txt");
		Memory testMem = new Memory();
		Storage.checkForFileExists(testMem);
		
		testMem_getTask(testMem);
		testMem_getDeadline(testMem);
		testMem_getEvent(testMem);
	}
	
	
	private static void saveToStorage_IO(){
		Storage.setSaveFileAddress("");
		Storage.saveToStorage(new Memory());
	}
	
	private static void testMem_getTask(Memory mem){
		GenericEvents task = new GenericEvents("Task", "I am hungry", "very hungry, now get me some chicken rice.");
		assertEquals("test if they are the same", testTwoTasks(mem.get(0), task), true); 
		
	}
	
	private static void testMem_getDeadline(Memory mem){
		Calendar end = new GregorianCalendar();
		end.set(2014, 1, 27, 23, 59); 
		
		GenericEvents deadline = new GenericEvents("Deadline", "Still very hungry", "please call dominos", end);
		assertEquals("test if they are the same", testTwoDeadlines(mem.get(1), deadline), true); 

		
	}
	
	private static void testMem_getEvent(Memory mem){
		Calendar end = new GregorianCalendar();
		Calendar start = new GregorianCalendar();
		start.set(2014, 1, 27, 23, 0);
		end.set(2014,  1, 28, 2, 0);
		
		GenericEvents event = new GenericEvents("Event", "Supper anybody?", "MacDonald is cool too", start, end, 0);
		assertEquals("test if they are the same", testTwoEvents(mem.get(2), event), true); 
		
	}
	
	
	private static boolean testTwoTasks(GenericEvents a, GenericEvents b){
		return a.getType().equals(b.getType()) &&
				a.getDescription().equals(b.getDescription())&&
				a.getName().equals(b.getName());

	}
	
	private static boolean testTwoDeadlines(GenericEvents a, GenericEvents b){
		return testTwoTasks(a,b)&&
				testTwoCalendar(a.getEnd(), b.getEnd());
	}
	
	private static boolean testTwoEvents(GenericEvents a, GenericEvents b){
		return testTwoDeadlines(a,b)&&
				testTwoCalendar(a.getStart(), b.getStart());
	}
	
	private static boolean testTwoCalendar(Calendar a, Calendar b){
		return a.get(Calendar.YEAR)== b.get(Calendar.YEAR)&&
				a.get(Calendar.MONTH) == b.get(Calendar.MONTH)&&
				a.get(Calendar.DATE)== b.get(Calendar.DATE);
	}

}
